﻿using CommunicationClass.Auxiliary;
using CommunicationClass.Client;
using CommunicationClass.Model;
using CommunicationClass.Server.Model;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Net.Sockets;
using System.Text;
using System.Threading;
using System.Windows.Forms;
using static CommunicationClass.Client.ClientCommunication;
using static CommunicationClass.Model.SendReceiveEnum;

namespace WindowsFormsApp1
{
    public partial class Form1 : Form
    {
        ClientCommunication tCPSend = new ClientCommunication("127.0.0.1", 7614, "127.0.0.1", 7615);
        public Form1()
        {
            InitializeComponent();
            tCPSend.LoginEvent += IsLogin;
            tCPSend.NoLogin += TCPSend_NoLogin;
            tCPSend.ReceiveMessageEvent += TCPSend_ReceiveMessageEvent;
            tCPSend.SendMessageEvent += TCPSend_SendMessageEvent;
            tCPSend.LoadUserData += TCPSend_LoadUserData;
        }


        private void TCPSend_LoadUserData(string data)
        {
            this.BeginInvoke(new Action(() => {
                textBox1.Text += "基本信息加载：" + data + "\r\n";
                textBox1.Select(this.textBox1.TextLength, 0);//光标定位到文本最后
                textBox1.ScrollToCaret();//滚动到光标处
            }));
        }

        private void TCPSend_SendMessageEvent(bool IsSend, string Data)
        {
            this.BeginInvoke(new Action(() => {
                if (IsSend)
                {
                    textBox1.Text += "发送成功,消息ID：" + Data + "\r\n";
                }
                else
                {
                    textBox1.Text += "发送失败,原因：" + Data + "\r\n";
                }

                textBox1.Select(this.textBox1.TextLength, 0);//光标定位到文本最后
                textBox1.ScrollToCaret();//滚动到光标处
            }));
        }

        private void TCPSend_ReceiveMessageEvent(MessageModel messageModel, SendTypeEnum MessageType)
        {
            this.BeginInvoke(new Action(() => {
                if (MessageType == SendTypeEnum.语音消息)
                {
                    Button bt = new Button();
                    bt.Tag = messageModel.MsgOtherContent;
                    bt.Text = messageModel.MsgTitle;
                    bt.Click += Bt_Click;
                    if (this.panel1.Controls.Count>0)
                    {
                        bt.Location = new Point(0, this.panel1.Controls[this.panel1.Controls.Count - 1].Location.Y + 30);
                    }
                    
                    this.panel1.Controls.Add(bt);
                }
                else
                {
                    textBox1.Text += "接收消息：发送人" + messageModel.MsgSendUserID + "接收人" + messageModel.MsgReceiveUserID + "\r\n";
                    textBox1.Select(this.textBox1.TextLength, 0);//光标定位到文本最后
                    textBox1.ScrollToCaret();//滚动到光标处
                }


            }));
        }

        private void Bt_Click(object sender, EventArgs e)
        {
            Button t = sender as Button;
            byte[] data = t.Tag as byte[];
            AudioHelper.Play(data, AudioPlayMode.Background);
        }

        private void TCPSend_NoLogin(bool IsSend, string Data)
        {
            this.BeginInvoke(new Action(() => {
                textBox1.Text += "未登录"+Data + "\r\n";
                textBox1.Select(this.textBox1.TextLength, 0);//光标定位到文本最后
                textBox1.ScrollToCaret();//滚动到光标处
            }));
        }




        private void button1_Click(object sender, EventArgs e)
        {

            tCPSend.Login(textBox2.Text, textBox4.Text);
        }

        public void IsLogin(bool isLogin, string Message)
        {
            this.BeginInvoke(new Action(() => {
                textBox1.Text += Message + "\r\n";
                textBox1.Select(this.textBox1.TextLength, 0);//光标定位到文本最后
                textBox1.ScrollToCaret();//滚动到光标处
            }));
        }

        private void button2_Click(object sender, EventArgs e)
        {
            MessageModel messageModel = new MessageModel()
            {
                MsgID = "412",
                MsgContent = "消息内容@！！##！",
                MsgTitle = "123",
                MsgSendUserID = textBox2.Text,
                MsgReceiveUserID = textBox3.Text,
                MsgSendeQuipment = new List<MsgLoginTypeEnum>() { MsgLoginTypeEnum.客户端 }
            };

    
                messageModel.MsgID = "sss";
                tCPSend.SendMessage(messageModel,ClientCommunication.SendTypeEnum.普通消息);
       

            
        }
        //CommunicationClass.Auxiliary.VoiceHelper ss;
        SoundRecorder recorder;
        string wavfile = "";
        private void 录音_Click(object sender, EventArgs e)
        {
            if (录音.Text == "录音录制")
            {
                录音.Text = "发送语音";
                //初始化
                try
                {
                    //
                    // 录音设置
                    //
                    if (recorder == null)
                    {
                        recorder = new SoundRecorder();
                    }
                    //
                    // 录音设置
                    //
                    wavfile = System.Environment.CurrentDirectory+"\\"+DateTime.Now.ToFileTimeUtc().ToString()+ ".AudioLog";
                    recorder.SetFileName(wavfile);
                    recorder.RecStart();
                }
                catch (Exception ex)
                {

                    textBox1.Text += ex.Message;
                }

            }
            else
            {
                录音.Text = "录音录制";
                if (recorder != null)
                {

                    recorder.RecStop();
                    recorder = null;

                    
                    byte[] data = FileUtil.FileToBytes(wavfile); ;
                        MessageModel messageModel = new MessageModel()
                        {
                            MsgID = "412",
                            MsgContent = "消息内容@！！##！",
                            MsgSendUserID = textBox2.Text,
                            MsgReceiveUserID = textBox3.Text,
                            MsgSendeQuipment = new List<MsgLoginTypeEnum>() { MsgLoginTypeEnum.客户端 },
                            MsgOtherContent = data
                        };
                        messageModel.MsgID = "s";
                        tCPSend.SendMessage(messageModel, ClientCommunication.SendTypeEnum.语音消息);


                   

                    
                }
            }
        }

    }
}
